clear;close all;clc;
Sine0_BB1 = 0;

freqs = [0 100:100:3000];
nturns=[linspace(0.1, 0.9, 6) linspace(1, 2, 24)];
% rad=0.2;
rad=0.1;
Norder=3;
thre = 0.03;
% onsets=[13.5 13 13.5 12.5 13 12.5 12.5 12.5 13 12 ...
% 12    12 13   12   8 14    12  12  16.5 13]
%
onsets = [14 18.5 12.5 15 12.5 21.5 13 12.5 12 12.5 ...
    12 12.5 12.5 12.5 15 12 12 12 12] % for different locations

period = 10;
fs = 44100;
% whos
plot_each =0;
Num_centers = 1;
% Proto_Num = 1.2;
% Proto_Num = 21;
Proto_Num = 32;

Angles = -90:10:90;
sides = {'Left','Right'};


f2=figure('position',[100 70 1600 800]);
for ifreql = 2:length(freqs)
    % for ifreql = length(freqs)
    % for ifreql = 1
    if plot_each==1
        f1=figure('position',[60 70 1600 700]);
    end
    freq = freqs(ifreql)
    tdur = 40/freq;
    for iangle = 1:length(Angles)
        % for iangle = length(Angles)
        % cc = 0;
        Angle = Angles(iangle);
        PString = num2str(Proto_Num);
        if Angle < 0
            ss = '-';
        else
            ss = '+';
        end
        [YY fs2]=audioread(['C:\Users\gaiy\OneDrive - Saint Louis University\Sound_loc\3Dio recordings\Protocol_' PString '_deg' ss num2str(abs(Angles(iangle))) '.mp3']);
        recorded_sound=resample(YY,441,320);

        if Sine0_BB1==0
            beginP = (1+(ifreql-1).*period).*fs+1;
            endP = (ifreql.*period-1).*fs; % the first period is BB noise
        else
            beginP = 1;
            endP = round(period.*fs); % the first period is BB noise
        end

        % figure
        % NL = length(recorded_sound(:,1));
        % tsig = (1:NL)./fs;
        % plot(tsig,recorded_sound(:,1));hold on;
        % for ifreql2 = 1:length(freqs)
        %     beginP2 = (1+(ifreql2).*period).*fs+1;
        %     endP2 = ((ifreql2+1).*period-2).*fs; % the first period is BB noise
        %     plot([1 1].*beginP2./fs+onsets(iangle),[-2 2],'b')
        %     plot([1 1].*endP2./fs+onsets(iangle),[-2 2],'r');
        %     grid on;
        %     % xlim([0 80])
        %     % pause
        % end
        % xlabel('Time (sec)')
        % sdf0
        recorded_sound=recorded_sound(onsets(iangle).*fs+beginP:onsets(iangle).*fs+endP,:);
        left_sound = recorded_sound(:,1);
        right_sound = recorded_sound(:,2);

        % figure;
        % plot(left_sound);hold on;plot(right_sound)
        % title([num2str(freq) ' Hz'])
        % pause(0.2)

        [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre,tdur);
        % normalization:
        % [SL1,SR1]=Normalize(SL,SR);
        % if Angle < 0
        %     L_or_R=1;
        % else
        L_or_R=2;
        % end
        SLR=Normalize_Both(SL,SR,L_or_R);
        [Y Ind] = min(abs(freq_pos - freq));
        X_P = [real(SLR(Ind,flag>0))' imag(SLR(Ind,flag>0))'];

        if plot_each==1
            figure(f1)
            subplot(4,5,iangle);
            plot(X_P(:,1),X_P(:,2),'k.');hold on;axis square
        end
        % end

        out = f_mycenter(X_P,rad);
        F_centers(iangle,:) = out.C1;

        if Angle < 0
            Pangle =['-' num2str(abs(Angles(iangle))) ];
        elseif Angle ==0
            Pangle =[num2str(abs(Angles(iangle))) ];
        else
            Pangle =['+' num2str(abs(Angles(iangle))) ];
        end
        if plot_each==1
            s=plot(F_centers(iangle,1),F_centers(iangle,2),'go');hold on;set(s,'linewidth',2)
            %         grid on
            set(gca,'Xtick',-1:0.5:1)
            set(gca,'Ytick',-1:0.5:1)
            title([num2str(freq) 'Hz;' Pangle]);
            axis([-1 1 -1 1])
        end

        figure(f2)
        % subplot(5,6,ifreql-1)
        subplot(3,5,ifreql-1)
        s=text(F_centers(iangle,1),F_centers(iangle,2),Pangle);hold on;set(s,'fontsize',7)
        hold on;
        axis([-1 1 -1 1])
        pause(0.01)
    end % angle

    hold on;
    plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
    plot([0,0,0],-1:1,'k','LineWidth',0.5)
    set(gca,'fontsize',10)
    xlabel('Re','fontsize',12)
    ylabel('Im','fontsize',12)
    hold on

    figure(f2)
    % subplot(5,6,ifreql-1)
    subplot(3,5,ifreql-1)
    plot(F_centers(:,1),F_centers(:,2),'r')
    title([num2str(freq) ' Hz'])
    axis([-1 1 -1 1]);axis square
    pause(0.1)
    %==============================
    out=f_get_spiral(F_centers,Norder,nturns(ifreql));
    x_spiral=out.x_spiral;
    y_spiral=out.y_spiral;
    % figure(f2)
    % subplot(5,6,ifreql-1)
    % plot(x_spiral,y_spiral,'color',[0.5 0.5 0.5]) ; % nturns crossings, including end point
    % axis square

    % eval(['save Model_centers\Freq_centers_sinu' num2str(freq) ' F_centers  x_spiral y_spiral'])
    % clear F_centers
end % freq




% figure;
% pc = 'bkrm';
% for ifreq = 1:4
%     s=plot([fliplr(-Angles) Angles],[fliplr(record_class{1}(ifreq,:)) record_class{2}(ifreq,:)]',pc(ifreq));hold on;
%     set(s,'linewidth',2)
%     if ifreq<3
%         set(s,'linestyle',':')
%     end
% end
% plot([-100 100],[-100 100],'g');
% legend('300 Hz',' 600 Hz',' 1500 Hz',' 3000 Hz','Perfect','Location','northwest')
% legend boxoff
% xlabel('True Angle (degrees)','fontsize',12)
% ylabel('classified Angle (degrees)','fontsize',12)
% title('Single Source')
